/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


package fl.location.core;

import fl.location.ui.Frame;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.game.GameCanvas;

/**
 *
 * @author Zeor
 */
public class CoreLoop extends GameCanvas implements Runnable {
    private Frame currentFrame;
    private boolean isPlaying;
    private long delay;

    public CoreLoop() {                
        super(false);        
        isPlaying = true;
        delay = 30;      
        setFullScreenMode(true);
        
    }    

    public void setFrame(Frame frame) {
//        if (currentFrame != null) {
//            currentFrame.deinit(this);
//        }
        currentFrame = frame;
//        if (currentFrame != null) {
//            currentFrame.init(this);
//        }
    }
    
    public void stop() {
        isPlaying = false;
    }

    public void run() {
        Graphics g = getGraphics();
        while (isPlaying == true) {
            if(isShown())
            {
                currentFrame.handleInput(getKeyStates());
                currentFrame.draw(g);
                flushGraphics();
            }
            try {
                Thread.sleep(delay);
            } catch (InterruptedException ex) {
                ex.printStackTrace();
            }
        }
    }
}
